package app.netant.book.txt263;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class Txt263Detail extends BaseDetail {

	public Txt263Detail(String netSite) {
		super(netSite);
		this.preHref = "http://www.txt263.com";
	}

	@Override
	public void analyseTitle() {
		Elements parts = this.select(".container.top20 .left .txtdetail .info h1");
		if(parts.size()>0){
			String title = parts.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".rar");
		}
	}

	@Override
	public void analyseTags() {
		Elements parts = this.select(".container.top20 .left .txtdetail .info .detail p");
		for(int i=0;i<parts.size();i++){
			String tags = parts.get(i).text();
			int idx = tags.indexOf("：");
			if(idx>0){
				String tagName = tags.substring(0,idx).trim();
				String tagValue = tags.substring(idx+1).trim();
				System.out.println("【Tag】:"+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
			}
		}
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select(".container.top20 .left .top20 .novelcon.top20 p");
		if(parts.size()>0){
			String memo = this.getString(parts.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		Elements parts = this.select(".container.top20 .left .top20 a#download");
		if(parts.size()>0){
			String url = parts.get(0).attr("href");
			if(url.startsWith("/")){
				url = this.preHref + url;
 			}
			System.out.println("【Url】:"+url);
			Txt263Detail detail = new Txt263Detail(url);
			String href = detail.stepHref();
			System.out.println("【Href】:"+href);	
			
			this.entity.setDownPath(href);
		}
	}
	
	public String stepHref(){
		Elements parts = this.select(".container.top20 .right.rbg .downs a.red");
		if(parts.size()>0){
			String href = parts.get(0).attr("href");
			if(href.startsWith("/")){
				href = this.preHref + href;
				return href;
			}
		}
		return null;
	}
	
	@Override
	public void analyseType() {
		Elements parts = this.select(".location.container.top20 a");
		if(parts.size()>1){
			String type = parts.get(1).text();
			int idx = type.indexOf("TXT");
			if(idx>0){
				type = type.substring(0,idx);
			}
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		Txt263Detail detail = new Txt263Detail("http://www.txt263.com/txt/kongjin.html");
		detail.test();
	}



}
